package com.software.comment.contorller;

import com.software.comment.clients.CommentClient;
import com.software.comment.config.PatternProperties;
import com.software.comment.pojo.Comment;
import com.software.comment.pojo.Result;
import com.software.comment.service.CommentService;
import com.software.comment.utils.SensitiveWordUtil;
import jakarta.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 周昊
 * @since 2024/12/16 11：34
 * @version 1.0
 */
@RestController
@RequestMapping("/comments")
@Validated
public class CommentController {
    private static final int MAX_COMMENT_LENGTH = 250;
    @Resource
    private CommentService commentService;
    @Resource
    private SensitiveWordUtil sensitiveWordUtil;
    @Resource
    private PatternProperties properties;

    @GetMapping("now")
    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat()));
    }

    @GetMapping("prop")
    public PatternProperties properties(){
        return properties;
    }

    @Autowired
    private CommentClient commentClient;


    /**
     * 添加评论
     *
     * @param comment 评论实体类
     * @return 成功或者失败的提示信息
     */
    @PostMapping
    public Result<?> addComment(@RequestBody Comment comment) {
        if(comment.getContent().length() > MAX_COMMENT_LENGTH ){
            return Result.error("评论字数超过250字！");
        }
        commentService.addComment(comment);
        return Result.success("评论成功");
    }

    /**
     * 添加回复评论
     *
     * @param comment 回复评论实体类
     * @return 成功或者失败的提示信息
     */
    @PostMapping("/reply")
    public Result<?> showReply(@RequestBody Comment comment) {
        if(comment.getContent().length() > MAX_COMMENT_LENGTH ){
            return Result.error("评论字数超过250字！");
        }
        commentService.replyComment(comment);
        return Result.success("回复成功");
    }


    /**
     * 展示评论，并进行敏感词过滤
     *
     * @param taskId 传入的课程id
     * @return 成功或者失败的提示信息
     */
    @GetMapping("/{taskId}")
    public Result<?> showCommentList(@PathVariable Long taskId) {
        List<Comment> comments = commentService.findCommentsByTaskId(taskId);

        if (comments!= null &&!comments.isEmpty()) {
            // 敏感词过滤
            List<Comment> filteredComments = new ArrayList<>();
            for (Comment c : comments) {
                String content = c.getContent();
                // 通过实例调用contains方法
                if (sensitiveWordUtil.contains(content)) {
                    c.setContent(sensitiveWordUtil.replace(content));
                }
                filteredComments.add(c);
            }
            return Result.success(filteredComments);
        } else {
            return Result.success("成功");
        }
    }

    /**
     * 删除评论
     *
     * @param id 传入的评论Id
     * @return 成功或者失败的提示信息
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteComment(@PathVariable Long id) {
        boolean isDeleted = commentService.deleteCommentById(id);
        if (isDeleted) {
            return Result.success();
        } else {
            return Result.error("");
        }
    }

    /**
     * 查找回复评论
     *
     * @param taskId 传入的课程id
     * @param parentCommentId 传入的父评论id
     * @return 成功或者失败的提示信息
     */
    @GetMapping("/reply/{taskId}/{parentCommentId}")
    public Result<?> getCommentsByTaskIdAndParentId(@PathVariable Long taskId, @PathVariable Long parentCommentId) {
        List<Comment> comments =commentService.findCommentsByTaskIdAndParentId(taskId, parentCommentId);
        if (comments != null && !comments.isEmpty()) {
            return Result.success(comments);
        }else {
            return Result.success();
        }
    }

    /**
     * 所属的评论置顶状态
     *
     * @param commentId 传入的评论ID
     * @return 成功或者失败的提示信息
     */
    @GetMapping("/top/{commentId}")
    public Result<?> getIsTopComment(@PathVariable Long commentId) {
        Integer isTopComment = commentService.getIsTopComment(commentId);
        if (isTopComment!= null) {
            return Result.success(isTopComment);
        }else {
            return Result.success();
        }
    }

    /**
     * 置顶评论
     *
     * @param commentId 需要置顶的评论ID
     * @return 成功或者失败的提示信息
     */
    @PatchMapping("/top/{commentId}")
    public Result<?> toTopComment(@PathVariable Long commentId) {
        Integer isTopComment = commentService.getIsTopComment(commentId);
        if (isTopComment== 0) {
            commentService.toTopComment(commentId);
            return Result.success(1);
        }else {
            commentService.cancelTopComment(commentId);
            return Result.success(0);
        }
    }

    /**
     * 查找某评论关联的评论
     *
     * @param commentId 传入的评论ID
     * @return 成功或者失败的提示信息
     */
    @GetMapping("/allRelatedComments/{commentId}")
    public Result<List<Comment>> getAllRelatedComments(@PathVariable Integer commentId) {
        List<Comment> relatedComments = commentService.findAllRelatedCommentsById(commentId);
        if (relatedComments!= null &&!relatedComments.isEmpty()) {
            return Result.success(relatedComments);
        } else {
            return Result.success(new ArrayList<>());
        }
    }
}